<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .content{
            width: 1024px;
            height: 500px;
            background-color: red;
            margin: 0 auto;
            overflow: hidden;
        }
        .banner{
            width: 1024px;
            height: 500px;
            display: flex;
            transition: all 500ms;
        }
        img{
            width: 1024px;
            height: 500px;
            flex-shrink: 0;
        }
        .btn{
            width: 1024px;
            height: 30px;
            margin: 0 auto;
            cursor: pointer;
        }
        .btn button{
            width: 100px;
            height: 30px;
        }
    </style>
</head>
<body>
    <div class="content">
        <div class="banner">
            <img src="./image/05.webp" alt="">
            <img src="./image/01.jpeg" alt="">
            <img src="./image/02.webp" alt="">
            <img src="./image/03.webp" alt="">
            <img src="./image/04.webp" alt="">
            <img src="./image/05.webp" alt="">
            <img src="./image/01.jpeg" alt="">
        </div>
    </div>
    <div class="btn">
        <button class="pre">上一页</button>
        <button class="next">下一页</button>
    </div>
    <script>
        // 常量和元素的获取
        const maxWidth = 1024
        const banner = document.querySelector('.banner')
        const pre = document.querySelector('.pre')
        const next = document.querySelector('.next')
        const content = document.querySelector('.content')
        const btn = document.querySelector('.btn')
        const himgLength = banner.children.length
        let currentIndex = 1
        let preIndex = 1
        let timer = null

        banner.style.transform = `translateX(${-maxWidth}px)`
        // 像下一页滚动的函数
        function nextMove(){
            preIndex = currentIndex
            currentIndex++
            if(currentIndex <= himgLength-2){
                banner.style.transition = 'all 500ms'
                banner.style.transform = `translateX(${-currentIndex*1024}px)`
            }else{
                banner.style.transform = `translateX(${-currentIndex*1024}px)`
                currentIndex = 1
                setTimeout(() => {
                    banner.style.transition = 'none'
                    banner.style.transform = `translateX(${-currentIndex*1024}px)`
                },500)
            }
        }

        // 点击下一页
        next.addEventListener('click',nextMove)

        // 上一页滚动的函数
        function premove(){
            preIndex = currentIndex
            currentIndex--
            if(currentIndex < 1){
                banner.style.transform = `translateX(${-currentIndex*1024}px)`
                setTimeout(() => {
                banner.style.transition = 'none'
                currentIndex = himgLength-2
                banner.style.transform = `translateX(${-currentIndex*1024}px)`
                },500)
            }else{
                banner.style.transition = 'all 500ms'
                banner.style.transform = `translateX(${-currentIndex*1024}px)`
            }
        }

        // 点击上一页
        pre.addEventListener('click', premove)

        // 自动轮播
        autoPlay()
        function autoPlay(){
            timer = setInterval(nextMove,3000)
        }

        // 开始自动轮播
        function starPlay(){
            if(!timer){
                console.log('开始');
                autoPlay()
            }
        }

        // 取消自动轮播
        function cancelPlay(){
            if(timer){
                console.log('取消');
                clearInterval(timer)
                timer = null
            }
        }

        // 鼠标移入取消自动轮播
        content.addEventListener('mouseenter',cancelPlay)
        // 鼠标移出开始自动轮播
        content.addEventListener('mouseleave',starPlay)
        // 鼠标移入取消自动轮播
        btn.addEventListener('mouseenter',cancelPlay)
        // 鼠标移出开始自动轮播
        btn.addEventListener('mouseleave',starPlay)


    </script>
</body>
</html>